package com.wealth.scsj.p_app.view.activity.bankcardmodel;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.wealth.scsj.p_app.R;
import com.wealth.scsj.p_app.base.BindBankCardBase;
import com.wealth.scsj.p_app.bean.BaseBean;
import com.wealth.scsj.p_app.bean.BindBankCardPostBean;
import com.wealth.scsj.p_app.bean.SendBindBankCardCodePostBean;
import com.wealth.scsj.p_app.presenter.BindBankCardPresenter;
import com.wealth.scsj.p_app.utils.DialogUtils;
import com.wealth.scsj.p_app.utils.RxCountDown;
import com.wealth.scsj.p_app.utils.Tool;
import com.wealth.scsj.p_app.view.activity.BaseActivity;

import java.util.HashMap;

import es.dmoral.toasty.Toasty;
import okhttp3.RequestBody;
import rx.Subscriber;
import rx.functions.Action0;

/**
 * 验证银行卡信息
 */
public class VerificationCardActivity extends BaseActivity implements View.OnClickListener, BindBankCardBase.IView {


    private Button mNextBtn;
    private HashMap<String, String> map = new HashMap();
    /**
     * 杨**
     */
    private TextView mUserNameTv;
    /**
     * 62170024543526512
     */
    private TextView mBankCardNumTv;
    private TextView mBankCardNameTv;
    private TextView mBankOpenTv;
    /**
     * 预留手机号
     */
    private EditText mPhoneNumEdt;
    /**
     * 获取验证码
     */
    private Button mSendSmsBtn;
    /**
     * 验证码
     */
    private EditText mVerificationCodeEdt;
    private BindBankCardPresenter presenter;
    private String mPhoneNum;
    private String name;
    private String cardNo;
    private String bankName;
    private String bankOpen;
    private String cardType;


    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_verification_card);

        initView();
        initData();
        initListener();
        initToolbar();
        setToolbarTitle("验证银行卡信息");
        hasBack(true);

        presenter = new BindBankCardPresenter(this);

        //取出上个页面的hashMap
//        map = (HashMap) getIntent().getSerializableExtra("map");
        Logger.e(map.toString());

    }

    @Override
    protected void initView() {

        mNextBtn = (Button) findViewById(R.id.agree_btn);
        mUserNameTv = (TextView) findViewById(R.id.user_name_tv);
        mBankCardNumTv = (TextView) findViewById(R.id.Bank_card_num_tv);
        mBankCardNameTv = (TextView) findViewById(R.id.bank_card_name_tv);
        mBankOpenTv = (TextView) findViewById(R.id.bank_open_tv);
        mPhoneNumEdt = (EditText) findViewById(R.id.phone_num_edt);
        mSendSmsBtn = (Button) findViewById(R.id.send_sms_btn);
        mVerificationCodeEdt = (EditText) findViewById(R.id.verification_code_edt);

    }

    @Override
    protected void initListener() {
        mNextBtn.setOnClickListener(this);
        mSendSmsBtn.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        if (!map.isEmpty()) {
            Logger.e("String==>" + map.get("name"));
            name = map.get("name");
            mUserNameTv.setText(name);
            cardNo = map.get("cardNo");
            mBankCardNumTv.setText(cardNo);
            bankName = map.get("bankName");
            mBankCardNameTv.setText(bankName);
            bankOpen = map.get("bankOpen");
            mBankOpenTv.setText(bankOpen);
            cardType = map.get("cardType");
        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.agree_btn:
                mPhoneNum = mPhoneNumEdt.getText().toString().trim();
                String mVerificationCode = mVerificationCodeEdt.getText().toString().trim();
                if(!TextUtils.isEmpty(mVerificationCode)){
                    BindBankCardPostBean postBean = new BindBankCardPostBean(mVerificationCode
                            ,new BindBankCardPostBean.UserBankCardBean(cardNo,name,mPhoneNum,bankOpen,bankName,Integer.valueOf(cardType)));
                    String s = new Gson().toJson(postBean);
                    RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"), s);
                    presenter.deliverBindData(body);
                }else {
                    Toasty.info(this,"请输入验证码").show();
                }
//                SkipUtils.toPhoneCodeVerification(this, "bank");
                break;
            case R.id.send_sms_btn:
                mPhoneNum = mPhoneNumEdt.getText().toString().trim();
                if(Tool.isPhoneNumber(mPhoneNum)){
                    SendBindBankCardCodePostBean postBean = new SendBindBankCardCodePostBean(new SendBindBankCardCodePostBean.UserBankCardBean(mPhoneNum));
                    String s = new Gson().toJson(postBean);
                    RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"), s);
                    presenter.deliverSendSmsData(body);
                }else {
                    mPhoneNumEdt.setError("请输入正确的手机号");
                }
            break;
            default:
                break;
        }
    }


    @Override
    public void SendSmsSuccessMsg(String s, BaseBean addDABean) {
        RxCountDown.countdown(60)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {//开始计时
                        mSendSmsBtn.setClickable(false);
                    }
                })
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {//计时完成
                        mSendSmsBtn.setClickable(true);
                        mSendSmsBtn.setText("获取验证码");

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Integer count) {//当前计时
                        mSendSmsBtn.setText(count+"s");
                    }
                });

    }

    @Override
    public void BindSuccessMsg(String s, BaseBean deleteDABean) {
        //添加银行卡弹窗
        DialogUtils.getInstance()
                .with(this)//上下文
                .setlayoutId(R.layout.add_bank_cark_layout)//布局文件
                .setlayoutPosition(Gravity.CENTER)//位置
                .setlayoutPading(222, 0, 222, 0)//与屏幕距离
                .show();
    }

    @Override
    public void showDAErrorMsg(String s) {
        Toasty.error(this,s).show();
    }
}
